package com.wyq.new_springboot.config.shiro;

import com.wyq.new_springboot.entity.*;
import com.wyq.new_springboot.service.*;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义的realm
 * @Author weiyaqiang
 * @create 2021/4/8 16:34
 */
public class MyShiroRealm extends AuthorizingRealm {

    @Autowired
    ITUserService itUserService;
    @Autowired
    ITRoleService itRoleService;
    @Autowired
    ITUserRoleService itUserRoleService;
    @Autowired
    ITPermisService itPermisService;
    @Autowired
    ITRolePermisService itRolePermisService;
    /**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        String userName = (String)principalCollection.getPrimaryPrincipal();
        List<TUser> users = itUserService.selectByColumn("user_name", userName);
        if(users.size()==0){
            return simpleAuthorizationInfo;
        }else{
            TUser user=users.get(0);
            Integer id = user.getId();
            List<TUserRole> list = itUserRoleService.getById(id);
            List<Integer> roleIds = list.stream().map(TUserRole::getRoleId).collect(Collectors.toList());
            Collection<TRole> tRoles = itRoleService.listByIds(roleIds);
            for (TRole tRole : tRoles) {
                String roleName = tRole.getRoleName();
                //添加角色
                simpleAuthorizationInfo.addRole(roleName);
            }
            //根据身份ids，查询出身份权限表中的
            Collection<TRolePermis> tRolePermis = itRolePermisService.listByIds(roleIds);
            List<Integer> permisIds = tRolePermis.stream().map(TRolePermis::getPermisId).collect(Collectors.toList());
            //查询出所有的权限
            Collection<TPermis> tPermis = itPermisService.listByIds(permisIds);
            for (TPermis t:tPermis) {
                //添加权限
                simpleAuthorizationInfo.addStringPermission(t.getPermisName());
            }

        }
        return simpleAuthorizationInfo;
    }

    /**
     * 认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username = authenticationToken.getPrincipal().toString();
        List<TUser> users = itUserService.selectByColumn("user_name", username);
        TUser tUser=null;
        if(users.size()>0){
            tUser = users.get(0);
        }
        if(tUser == null){
            return null;
        }else{
            SimpleAuthenticationInfo simpleAuthenticationInfo=
                    new  SimpleAuthenticationInfo(username,tUser.getUserPassword(),getName());
            return simpleAuthenticationInfo;
        }

    }
}
